﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Test.usercontrol
{
    public partial class PicUpLoad : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /***************** 保存图片的代码 ********************/
        /*
        protected void Button1_Click(object sender, EventArgs e)
        {
            #region 图片信息

            // 图片及文件信息
            string[] fileControlName = HttpContext.Current.Request.Files.AllKeys;

            string[] smallurlfile = null;        // 小图
            string[] bigurlfile = null;          // 大图
            string[] filesize = null;            // 图片大小

            if (HttpContext.Current.Request.Files.Count > 0)
            {
                string temp_smallurlfile = Vincent._Request.GetStringForm("hSmallurlfile", "");     // 小图
                string temp_bigurlfile = Vincent._Request.GetStringForm("hBigurlfile", "");         // 大图
                string temp_filesize = Vincent._Request.GetStringForm("hFilesize", "");            // 图片大小

                int num = 0;
                for (int i = 0; i < HttpContext.Current.Request.Files.Count; i++)
                {
                    HttpPostedFile file = HttpContext.Current.Request.Files[i];

                    if (!Vincent._Utility.Regex_IsContains(fileControlName[i], @"file[0-9]"))
                        continue;

                    num = Vincent._Utility.Regex_GetNumToString(fileControlName[i]) - 1;

                    if (file == null)
                        continue;
                    num++;

                    int textid = 0;
                    string _bigurlfile = "";
                    string _smallurlfile = "";
                    int _filesize = 0;
                    byte[] b = null;
                    bool isUploadSuccess = false;    // 是否上传成功

                    if (file.ContentLength > 0)
                    {
                        try
                        {
                            // 保存图片
                            string errormsg = "";
                            Upload(@"E:\Vincent\Upload", b, ref errormsg);

                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("添加资源-上传图片:" + ex);
                        }

                        // 本地测试用
                        //isUploadSuccess = true;
                        //_bigurlfile = "../images/main_03.gif";
                        //_smallurlfile = "../images/main_03.gif";
                        if (isUploadSuccess)
                        {
                            temp_bigurlfile += (_bigurlfile + "|");
                            temp_smallurlfile += (_smallurlfile + "|");
                            temp_filesize += (_filesize + "|");
                        }
                    }
                }

                smallurlfile = Vincent._Utility.GetStringBySplit(temp_smallurlfile, '|');      // 小图
                bigurlfile = Vincent._Utility.GetStringBySplit(temp_bigurlfile, '|');          // 大图
                filesize = Vincent._Utility.GetStringBySplit(temp_filesize, '|');             // 图片大小
            }

            #endregion

        }

        public static bool Upload(string saveFilePath, byte[] files, ref string errormsg)
        {
            bool IsTrue = false;
            if (Vincent._IO.File.CreateDirectory(System.IO.Path.GetDirectoryName(saveFilePath)))
            {
                System.IO.FileStream fileStream = null;
                try
                {
                    fileStream = new System.IO.FileStream(saveFilePath, System.IO.FileMode.CreateNew);
                    fileStream.Write(files, 0, files.Length);
                    IsTrue = true;
                }
                catch (Exception exp) { IsTrue = false; errormsg = exp.Message; }
                finally
                {
                    if (fileStream != null)
                    {
                        fileStream.Close();
                        fileStream.Dispose();
                    }
                }
            }
            return IsTrue;
        }
        */

    }
}